package com.real.operationalcontrols.企业列表;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.real.operationalcontrols.R;
import com.real.operationalcontrols.rxhttp.MyDialogObserver;
import com.real.operationalcontrols.rxhttp.NetworkUrl;
import com.real.operationalcontrols.rxhttp.RxHttpManger;
import com.real.operationalcontrols.基类.BaseActivity;
import com.real.operationalcontrols.实体类.WaringListBean;
import com.real.operationalcontrols.适配器.BaoJingAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CancelAdapt;

public class WaringHistoryActivity extends BaseActivity implements OnDateSetListener,
        CancelAdapt {

    @BindView(R.id.bar_back_btn)
    ImageView barBackBtn;
    @BindView(R.id.bar_title)
    TextView barTitle;
    @BindView(R.id.bar_check_btn)
    TextView barCheckBtn;
    @BindView(R.id.start_time_btn)
    TextView startTimeBtn;
    @BindView(R.id.finsh_time_btn)
    TextView finshTimeBtn;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.frash)
    SmartRefreshLayout frash;
    String id;
    long start = 0;
    long finsh = 0;
    int checkType;
    BaoJingAdapter adapter;
    TimePickerDialog timePickerDialog;
    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf_day = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    List<WaringListBean.TasklistBean> data = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_waring_history);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_waring_history;
    }

    @Override
    protected void getData() {
        barTitle.setText("(水质)报警历史数据");
        id = getIntent().getStringExtra("id");
        //过去一月
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        start = c.getTimeInMillis();
        finsh = System.currentTimeMillis();
        finshTimeBtn.setText(sdf_day.format(finsh));
        startTimeBtn.setText(sdf_day.format(start));
        initFresh();
        initAdapter();
        initData();
    }

    private void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("username", NetworkUrl.USERNAME);
        RxHttpManger.getInstance().get(this, NetworkUrl.TASK_LIST, map,
                new MyDialogObserver(this) {
                    @Override
                    public void onSuccess(String response) {
                        WaringListBean bean = new Gson().fromJson(response, WaringListBean.class);
                        if (bean.isSuccess()) {
                            data.clear();
                            for (int i = 0; i < bean.getTasklist().size(); i++) {
                                if (bean.getTasklist().get(i).getStatus().contains("完成")) {
                                    data.add(bean.getTasklist().get(i));
                                }
                            }
                            adapter.setData(data);
                        } else {
                            ToastUtils.showShort(bean.getMessage());
                        }
                    }
                });
    }

    private void initAdapter() {
        adapter = new BaoJingAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        rv.setLayoutManager(layoutManager);
        rv.setAdapter(adapter);
        rv.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL));
    }

    @OnClick({R.id.bar_back_btn, R.id.start_time_btn, R.id.finsh_time_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back_btn:
                finish();
                break;
            case R.id.start_time_btn:
                checkType = 0;
                initTimePickView(Type.YEAR_MONTH_DAY);
                break;
            case R.id.finsh_time_btn:
                checkType = 1;
                initTimePickView(Type.YEAR_MONTH_DAY);
                break;
        }
    }

    private void initTimePickView(Type type) {
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;
        timePickerDialog = new TimePickerDialog.Builder()
                .setCallBack(this)
                .setCancelStringId("取消")
                .setSureStringId("确认")
                .setTitleStringId("选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setHourText("时")
                .setMinuteText("分")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis() - tenYears)
                .setMaxMillseconds(System.currentTimeMillis())
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(getResources().getColor(R.color.colorTitle))
                .setType(type)
                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.colorTitle))
                .setWheelItemTextSize(12)
                .build();
        timePickerDialog.show(getSupportFragmentManager(), type.toString());
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        switch (checkType) {
            case 0://开始
                start = millseconds;
                break;
            case 1://结束
                finsh = millseconds;
                break;
        }
        finshTimeBtn.setText(sdf_day.format(finsh));
        startTimeBtn.setText(sdf_day.format(start));
        if (start >= finsh) {
            ToastUtils.showShort("时间选择不正确");
        } else {
            initData();
        }
    }

    private void initFresh() {
        //刷新
        frash.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                    }
                }, 300);
                refreshLayout.finishRefresh(true);//刷新完成
            }
        });
    }
}
